package com.mchsdk.paysdk.http.thirdlogin;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.mchsdk.paysdk.common.Constant;
import com.mchsdk.paysdk.entity.UserLogin;
import com.mchsdk.paysdk.http.RequestUtil;
import com.mchsdk.paysdk.utils.MCErrorCodeUtils;
import com.mchsdk.paysdk.utils.MCLog;

import org.json.JSONException;
import org.json.JSONObject;

public class ThirdLoginRequest {

    private static final String TAG = "ThirdLoginRequest";

    HttpUtils http;
    Handler mHandler;
    String account;

    private boolean isYKLogin;

    public void setYKLogin(boolean YKLogin) {
        isYKLogin = YKLogin;
    }

    public ThirdLoginRequest(Handler mHandler) {
        isYKLogin = false;
        http = new HttpUtils();
        if (null != mHandler) {
            this.mHandler = mHandler;
        }
    }

    public void post(String url, RequestParams params) {
        if (TextUtils.isEmpty(url) || null == params) {
            MCLog.e(TAG, "fun#post url is null add params is null");
            return;
        }
        MCLog.e(TAG, "fun#post url = " + url);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String response = RequestUtil.getResponse(responseInfo);
                UserLogin loginSuccess = new UserLogin();
                try {
                    JSONObject json = new JSONObject(response);
                    int status = json.optInt("status");

                    if (status == 200 || status == 1) {
                        loginSuccess.setLoginStatus("1");
                        loginSuccess.setLoginMsg(json.optString("return_msg"));
                        loginSuccess.setAccountUserId(json.optString("user_id"));
                        loginSuccess.setSign(json.optString("sign"));
                        loginSuccess.setUserName(json.optString("account"));
                        loginSuccess.setToken(json.optString("token"));
                        loginSuccess.setLoginMsg("登录成功");
                        loginSuccess.setIs_uc(json.optString("is_uc", "0"));
                        loginSuccess.setUserName(json.optString("account"));
                        loginSuccess.setPassword(json.optString("account"));
                        loginSuccess.setYKLogin(isYKLogin);
                        noticeResult(Constant.USER_THIRD_PARAMS_SUCCESS, loginSuccess);
                    } else {
                        loginSuccess.setLoginStatus("-1");
                        String msg;
                        if (!TextUtils.isEmpty(json.optString("return_msg"))) {
                            msg = json.optString("return_msg");
                        } else {
                            msg = MCErrorCodeUtils.getErrorMsg(status);
                        }
                        MCLog.e(TAG, "msg:" + msg);
                        noticeResult(Constant.USER_THIRD_PARAMS_FAIL, msg);
                    }
                } catch (JSONException e) {
                    noticeResult(Constant.USER_THIRD_PARAMS_FAIL, "解析数据异常");
                } catch (Exception e) {
                    noticeResult(Constant.USER_THIRD_PARAMS_FAIL, "解析数据异常");
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                MCLog.e(TAG, "onFailure code =" + error.getExceptionCode() + " message =" + msg);
                noticeResult(Constant.USER_THIRD_PARAMS_FAIL, "登录失败");
            }
        });
    }

    private void noticeResult(int type, Object obj) {
        Message msg = new Message();
        msg.what = type;
        msg.obj = obj;
        if (null != mHandler) {
            mHandler.sendMessage(msg);
        }
    }
}
